import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import api from '../middleware/api';
import logger from 'redux-logger'
import rootReduce from '../reducers';

const middlewares = [];

middlewares.push(thunk)
middlewares.push(api)
middlewares.push(logger)

const finalCreateStore = compose(
    applyMiddleware(...middlewares)
)(createStore);

export function configureStore(initialState) {
    const store = finalCreateStore(rootReduce, initialState);

    if (module.hot) {
        module.hot.accept('../reducers', () => {
            const nextRootReduce = require('../reducers');
            store.replaceReducer(nextRootReduce);
        });
    }
    return store;
}